package com.bbx.taxi.client.Util;
import java.io.File;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.bbx.taxi.client.DB.AddressDB;
import com.bbx.taxi.client.DB.ContactsDB;
import com.bbx.taxi.client.DB.CouponDB;
import com.bbx.taxi.client.DB.JmsgDB;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.DB.RecommenDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderLocationsDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;

import android.content.Context;
import android.os.Environment;

/** * 本应用数据清除管理器 */
public class DataCleanManager {
    /** * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context */
    public static void cleanInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }

    /** * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context */
    public static void cleanDatabases(Context context) {
//        deleteFilesByDirectory(new File("/data/data/"
//                + context.getPackageName() + "/databases"));
//    	new MyOrderDB(context).onDelete();
//    	new MyOrderingDB(context).onDelete();
//    	new MyOrderLocationsDB(context).onDelete();
//    	new AddressDB(context).onDelete();
//    	new ContactsDB(context).onDelete();
//    	new CouponDB(context).onDelete();
//    	new JmsgDB(context).onDelete();
    	//线路Id不清楚
//    	new LineDB(context).onDelete();
//    	new RecommenDB(context).onDelete();
    	
    }

    /**
     * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
     * context
     */
    public static void cleanSharedPreference(Context context) {
        deleteFilesByDirectory(new File("/data/data/"
                + context.getPackageName() + "/shared_prefs"));
    	String str_line_num = SharedPreUtil.getStringValue(context, SharedPreEncryptUtil.list_line_num, "");
    	String str_line_cn = SharedPreUtil.getStringValue(context, SharedPreEncryptUtil.list_line_cn, "");
    	String str_incity_type = SharedPreUtil.getStringValue(context,	SharedPreEncryptUtil.list_incity_type, "");
		String str_incity_phone = SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.list_incity_phone, "");
		String str_incity_cn = SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.list_incity_cn, "");
        SharedPreEncryptUtil.clearAll(context);
        //不清空引导页
        SharedPreEncryptUtil.putBooleanValue(context, SharedPreEncryptUtil.isFirstIn, false);
        //不清空路线
        SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_line_num,str_line_num);
		SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_line_cn,str_line_cn);
		//不清空市内
		SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_type,str_incity_type);
		SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_phone,str_incity_phone);
		SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_cn,str_incity_cn);
        MyApplication mApplication=MyApplication.getInstance();
        mApplication.setUid("");
        mApplication.setToken("");
        mApplication.authority=SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.authority, "");
        mApplication.real_name=SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.real_name, "");
        mApplication.user_name=SharedPreUtil.getStringValue(context,SharedPreEncryptUtil.user_name, "");
        
    }

    /** * 按名字清除本应用数据库 * * @param context * @param dbName */
    public static void cleanDatabaseByName(Context context, String dbName) {
        context.deleteDatabase(dbName);
    }

    /** * 清除/data/data/com.xxx.xxx/files下的内容 * * @param context */
    public static void cleanFiles(Context context) {
        deleteFilesByDirectory(context.getFilesDir());
    }

    /**
     * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
     * context
     */
    public static void cleanExternalCache(Context context) {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            deleteFilesByDirectory(context.getExternalCacheDir());
        }
    }
    public static void clearAllCache(Context context) {
        deleteDir(context.getCacheDir());
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
            deleteDir(context.getExternalCacheDir());
        }  
    }
    private static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
    /** * 清除自定义路径下的文件，使用需小心，请不要误删。而且只支持目录下的文件删除 * * @param filePath */
    public static void cleanCustomCache(String filePath) {
        deleteFilesByDirectory(new File(filePath));
    }

    /** * 清除本应用所有的数据 * * @param context * @param filepath */
    public static void cleanApplicationData(Context context, String... filepath) {
        cleanInternalCache(context);
        cleanExternalCache(context);
        cleanDatabases(context);
        cleanSharedPreference(context);
        cleanFiles(context);
        for (String filePath : filepath) {
            cleanCustomCache(filePath);
        }
    }

    /** * 删除方法 这里只会删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理 * * @param directory */
    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }
}